home *** CD-ROM | disk | FTP | other *** search
- /* Revision History:
-
- 94/01/14 aih
- - added to function to calculate a rectangle's area
-
- 93/11/07 aih
- - made some parameters const
-
- 91/05/14 AIH
- - Fixed mistake in function for moving rectangles
-
- 91/05/12 AIH
- - Rectangles are positioned on the current screen, not the main screen
-
- 91/04/26 AIH
- - Added function to move a rectangle
-
- 91/03/20 AIH
- - Sometime earlier this month I added functions to convert a rectangle
- to global or local coordinates
- - RectValid always validates the rectangle, not just when debugging,
- since other code relies on RectValid to work all the time.
-
- 91/01/20 AIH
- - Uses main screen instead of GetGrayRgn for positioning a
- rectangle in the screen
-
- 91/01/05 Ari Halberstadt (AIH)
- - Inserted this standard header in all files */
-
- #include "ScreenLib.h"
- #include "RectangleLib.h"
-
- /* true if rectangle is valid */
- Boolean RectValid(const Rect *r)
- {
- return(r->top <= r->bottom && r->left <= r->right);
- }
-
- /* convert rectangle in current port's coordinates to global coordinates */
- void RectLocalToGlobal(Rect *r)
- {
- require(RectValid(r));
- LocalToGlobal(&((Point *) r)[0]);
- LocalToGlobal(&((Point *) r)[1]);
- }
-
- /* convert rectangle in global coordinates to current port's coordinates */
- void RectGlobalToLocal(Rect *r)
- {
- require(RectValid(r));
- GlobalToLocal(&((Point *) r)[0]);
- GlobalToLocal(&((Point *) r)[1]);
- }
-
- /* convert rectangle in specified port's coordinates to global coordinates */
- void RectPortToGlobal(Rect *r, GrafPtr port)
- {
- GrafPtr oldport;
-
- GetPort(&oldport);
- SetPort(port);
- RectLocalToGlobal(r);
- SetPort(oldport);
- }
-
- /* convert rectangle in global coordinates to specified port's coordinates */
- void RectGlobalToPort(Rect *r, GrafPtr port)
- {
- GrafPtr oldport;
-
- GetPort(&oldport);
- SetPort(port);
- RectGlobalToLocal(r);
- SetPort(oldport);
- }
-
- /* return height of rectangle */
- short RectHeight(const Rect *r)
- {
- require(RectValid(r));
- return(r->bottom - r->top);
- }
-
- /* return width of rectangle */
- short RectWidth(const Rect *r)
- {
- require(RectValid(r));
- return(r->right - r->left);
- }
-
- /* return area of rectangle */
- long RectArea(const Rect *r)
- {
- return(RectWidth(r) * RectHeight(r));
- }
-
- /* true if r1 is completely contained within r2 */
- Boolean RectWithin(const Rect *r1, const Rect *r2)
- {
- require(RectValid(r1));
- require(RectValid(r2));
- return(r1->left >= r2->left && r1->right <= r2->right &&
- r1->top >= r2->top && r1->bottom <= r2->bottom);
- }
-
- /* true if r1 intersects r2 */
- Boolean RectIntersect(const Rect *r1, const Rect *r2)
- {
- require(RectValid(r1));
- require(RectValid(r2));
- ensure(false); /* body of this function hasn't been written yet */
- }
-
- /* Position rectangle r2 within rectangle r1. To position in the middle
- horizontally and 1/3 of the way from the top, use h=2 and v=3. Point
- is set to the top left coordinates to which r1 should be moved. */
- void RectPosition(const Rect *r1, const Rect *r2, short h, short v, Point *pt)
- {
- require(RectValid(r1));
- require(RectValid(r2));
- require(h > 0);
- require(v > 0);
- pt->h = r1->left + (RectWidth(r1) - RectWidth(r2)) / h;
- pt->v = r1->top + (RectHeight(r1) - RectHeight(r2)) / v;
- }
-
- /* position the rectangle within the current screen */
- void RectPositionInScreen(const Rect *r, short h, short v, Point *pt)
- {
- Rect scrn;
-
- ScreenGrayRect(&scrn);
- RectPosition(&scrn, r, h, v, pt);
- }
-
- /* move the rectangle to the specified position */
- void RectMove(Rect *r, short h, short v)
- {
- short width, height;
-
- require(RectValid(r));
- width = RectWidth(r);
- height = RectHeight(r);
- r->bottom = v + height;
- r->right = h + width;
- r->top = v;
- r->left = h;
- ensure(RectValid(r));
- }
-
-
-